package cn.com.thingo.hyintelligent.ui.activity;

import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.AirConditionInfo;
import com.th.opensdk.openapi.NotificationInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.AirConditionControlImportContract;
import cn.com.thingo.hyintelligent.presenter.AirConditionControlImportPresenter;
import cn.com.thingo.hyintelligent.ui.receiver.THOpenReceiver;

/**
 * 导入的空调控制面板
 */

public class AirConditionControlImportActivity extends BaseActivity<AirConditionControlImportPresenter> implements AirConditionControlImportContract.View {
    @BindView(R.id.air_condition_mode_name)
    TextView tvMode;
    @BindView(R.id.air_condition_temperature_less)
    TextView tvTempLess;
    @BindView(R.id.air_condition_temperature_display)
    TextView tvTempDisplay;
    @BindView(R.id.air_condition_temperature_plus)
    TextView tvTempPlus;
    @BindView(R.id.air_condition_open)
    ImageView ivOpen;
    @BindView(R.id.air_condition_close)
    ImageView ivClose;
    @BindView(R.id.air_condition_mode_cold)
    TextView tvModeCold;
    @BindView(R.id.air_condition_mode_hot)
    TextView tvModeHot;
    @BindView(R.id.air_condition_mode_dehumidification)
    TextView tvModeDehumid;
    @BindView(R.id.air_condition_mode_wind)
    TextView tvModeWind;
    @BindView(R.id.air_condition_speed_high)
    TextView tvSpeedHigh;
    @BindView(R.id.air_condition_speed_middle)
    TextView tvSpeedMiddle;
    @BindView(R.id.air_condition_speed_low)
    TextView tvSpeedLow;
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;


    private AirConditionInfo airConditionInfo;
    private List<TextView> modeList = new ArrayList<>();
    private List<TextView> windList = new ArrayList<>();
    private THOpenReceiver receiver;


    @Override
    protected int getLayout() {
        return R.layout.activity_air_condition_control_import;
    }

    @Override
    protected void initEventAndData() {
        int position = getIntent().getIntExtra("air_condition_position", -1);
        int currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        if (position != -1) {
            List<AirConditionInfo> airConditions = THOpenSDK.getInstance().getAirConditionList();
            List<AirConditionInfo> airConditionInfos = new ArrayList<>();
            if (airConditions != null && airConditions.size() > 0) {
                for (int i = 0; i < airConditions.size(); i++) {
                    if (currentRoomId == airConditions.get(i).getRoomId()) {
                        airConditionInfos.add(airConditions.get(i));
                    }
                }
            }
            airConditionInfo = airConditionInfos.get(position);
            App.getInstance().setCurrentAirConditionId(airConditionInfo.getControlId());

        }
        initView();
        initData();
        initReceiver();
    }

    private void initData() {
        notifyAirCondition();
    }

    @Override
    protected void onDestroy() {
        App.getInstance().setCurrentAirConditionId(-1L);
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    private void initReceiver() {
        receiver = new THOpenReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(NotificationInfo.BROADCAST_ALARM_MESSAGE_UPLOAD);
        filter.addAction(NotificationInfo.BROADCAST_DATA_UPDATED_FINISH);
        filter.addAction(NotificationInfo.BROADCAST_DATA_UPDATED_START);
        filter.addAction(NotificationInfo.BROADCAST_DEVICE_STATE_UPLOAD);
        filter.addAction(NotificationInfo.BROADCAST_GATEWAY_STATUS_CHANGED);
        registerReceiver(receiver, filter);

    }


    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        tvTitle.setText(airConditionInfo.getDeviceName());
        modeList.add(tvModeCold);
        modeList.add(tvModeHot);
        modeList.add(tvModeDehumid);
        modeList.add(tvModeWind);
        windList.add(tvSpeedHigh);
        windList.add(tvSpeedMiddle);
        windList.add(tvSpeedLow);
    }


    @OnClick({R.id.toolbar_left, R.id.air_condition_temperature_less, R.id.air_condition_temperature_plus, R.id.air_condition_open, R.id.air_condition_close,
            R.id.air_condition_mode_cold, R.id.air_condition_mode_hot, R.id.air_condition_mode_dehumidification, R.id.air_condition_mode_wind,
            R.id.air_condition_speed_high, R.id.air_condition_speed_middle, R.id.air_condition_speed_low})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.air_condition_temperature_less:
                airConditionInfo.turnDownTemperature();
                break;
            case R.id.air_condition_temperature_plus:
                airConditionInfo.turnUpTemperature();
                break;
            case R.id.air_condition_open:
                airConditionInfo.turnOn();
                break;
            case R.id.air_condition_close:
                airConditionInfo.turnOff();
                break;
            case R.id.air_condition_mode_cold:
                airConditionInfo.changeMode(AirConditionInfo.MODE_COLD);
                break;
            case R.id.air_condition_mode_hot:
                airConditionInfo.changeMode(AirConditionInfo.MODE_HEAT);
                break;
            case R.id.air_condition_mode_dehumidification:
                airConditionInfo.changeMode(AirConditionInfo.MODE_DEHUMIDIFY);
                break;
            case R.id.air_condition_mode_wind:
                airConditionInfo.changeMode(AirConditionInfo.MODE_WIND);
                break;
            case R.id.air_condition_speed_high:
                airConditionInfo.changeWindSpeed(AirConditionInfo.WIND_SPEED_HIGH);
                break;
            case R.id.air_condition_speed_middle:
                airConditionInfo.changeWindSpeed(AirConditionInfo.WIND_SPEED_MEDIUM);
                break;
            case R.id.air_condition_speed_low:
                airConditionInfo.changeWindSpeed(AirConditionInfo.WIND_SPEED_LOW);
                break;

        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    private void cleanMode() {
        if (modeList.size() > 0) {
            for (int i = 0; i < modeList.size(); i++) {
                TextView textView = modeList.get(i);
                textView.setTextColor(getResources().getColor(R.color.gray_3));
                switch (textView.getText().toString().trim()) {
                    case "制冷":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold_un), null, null);
                        break;
                    case "制热":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot_un), null, null);
                        break;
                    case "除湿":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_dehumidification_un), null, null);
                        break;
                    case "送风":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_mode_wind_un), null, null);
                        break;
                }

            }
        }
    }

    private void cleanWind() {
        if (windList.size() > 0) {
            for (int i = 0; i < windList.size(); i++) {
                TextView textView = windList.get(i);
                textView.setTextColor(getResources().getColor(R.color.gray_3));
                switch (textView.getText().toString().trim()) {
                    case "高速":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_high_un), null, null);
                        break;
                    case "中速":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_middle_un), null, null);
                        break;
                    case "低速":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_low_un), null, null);
                        break;
                }
            }
        }
    }

    public void notifyAirCondition() {
        int mode = airConditionInfo.getMode();
        int power = airConditionInfo.getPower();
        int windSpeed = airConditionInfo.getWindSpeed();
        int temperature = airConditionInfo.getTemperature();
        tvTempDisplay.setText(String.valueOf(temperature).concat("℃"));
        switch (power) {
            case AirConditionInfo.POWER_OFF:
                ivOpen.setVisibility(View.VISIBLE);
                ivClose.setVisibility(View.GONE);
                cleanMode();
                cleanWind();
                tvTempDisplay.setText("");
                tvMode.setText("");
                break;
            case AirConditionInfo.POWER_ON:
                ivOpen.setVisibility(View.GONE);
                ivClose.setVisibility(View.VISIBLE);
                switch (mode) {
                    case AirConditionInfo.MODE_COLD:
                        tvMode.setText("制冷");
                        cleanMode();
                        tvModeCold.setTextColor(getResources().getColor(R.color.app_color));
                        tvModeCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                        break;
                    case AirConditionInfo.MODE_HEAT:
                        tvMode.setText("制热");
                        cleanMode();
                        tvModeHot.setTextColor(getResources().getColor(R.color.app_color));
                        tvModeHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                        break;
                    case AirConditionInfo.MODE_DEHUMIDIFY:
                        tvMode.setText("除湿");
                        cleanMode();
                        tvModeDehumid.setTextColor(getResources().getColor(R.color.app_color));
                        tvModeDehumid.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_dehumidification), null, null);
                        break;
                    case AirConditionInfo.MODE_WIND:
                        tvMode.setText("送风");
                        cleanMode();
                        tvModeWind.setTextColor(getResources().getColor(R.color.app_color));
                        tvModeWind.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_mode_wind), null, null);
                        break;
                }
                switch (windSpeed) {
                    case AirConditionInfo.WIND_SPEED_HIGH:
                        cleanWind();
                        tvSpeedHigh.setTextColor(getResources().getColor(R.color.app_color));
                        tvSpeedHigh.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_high), null, null);
                        break;
                    case AirConditionInfo.WIND_SPEED_MEDIUM:
                        cleanWind();
                        tvSpeedMiddle.setTextColor(getResources().getColor(R.color.app_color));
                        tvSpeedMiddle.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_middle), null, null);
                        break;
                    case AirConditionInfo.WIND_SPEED_LOW:
                        cleanWind();
                        tvSpeedLow.setTextColor(getResources().getColor(R.color.app_color));
                        tvSpeedLow.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_speed_low), null, null);
                        break;

                }
                break;
        }

    }
}
